package com.saibo.spider.gather;

import com.alibaba.fastjson.JSON;
import com.saibo.spider.commons.CodeUtil;
import com.saibo.spider.commons.RequestSogouVerifyCode;
import com.saibo.spider.main.MainControl;

import com.saibo.spider.utils.*;
import com.saibowisdom.base.api.aspirator.model.TaskEntry;
import org.apache.http.Header;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by gyt on 2017/4/5.
 */
public class MouldWeixinSearch implements CrawlerInterface{

    Map<String,Object> mso ;
    String url;
    String html;
    Submitable submitobj= null;

    public void startMission(String url, String mapInfo,Submitable submitobj) {
        this.submitobj = submitobj;
        this.url = url;
        mso = StringUtil.jsonToMap(mapInfo);
        int page = Integer.parseInt(mso.get("pageNumber").toString());

        for(int i = 0;i<page;i++){
            //请求
            requestUrl();
            //解析
            parse();

            try {
                Thread.sleep(10*1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

    public void requestUrl() {
        html = connect(url);


    }

    public String connect(String url){
        CloseableHttpResponse httpResponse = HttpRequestUtil.getRequestMethod(url, requestHeader());
        String html1 = HttpResponseUtil.getHtml(httpResponse);

        if (httpResponse.getStatusLine().getStatusCode() == 302) {
            // 出现验证码
            System.err.println("出现验证码,正在请求....");
            Header locationHeader = httpResponse.getFirstHeader("location");
            String location = "";
            if (locationHeader != null) {
                location = locationHeader.getValue();
                System.out.println(location);
            }

            String regEx = "/weixin\\?.*";
            Pattern pattern = Pattern.compile(regEx);
            Matcher matcher = pattern.matcher(url);
            matcher.find();

            String fromUrl = matcher.group(1);


            CloseableHttpResponse httpResponse1 = HttpRequestUtil.getRequestMethod(location, requestHeader());
            html1 = HttpResponseUtil.getHtml(httpResponse1);
            Map<String, String> mss = new HashMap<String, String>();
            mss.put("Accept", "image/webp,image/*,*/*;q=0.8");
            mss.put("Accept-Encoding", "gzip, deflate, sdch");
            mss.put("Accept-Language", "zh-CN,zh;q=0.8");
            mss.put("User-Agent",
                    "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
            mss.put("X-Requested-With", "XMLHttpRequest");
            String cookie = FileUtil.txt2String(new File("D:/sogou.txt"));// 读取保存在本地的已经登陆的Cookie信息
            mss.put("Cookie", cookie);
            mss.put("Referer", "http://weixin.sogou.com/antispider/?from=" + EncodeUtil.encodeURL(
                    fromUrl,
                    "UTF-8"));
            System.err.println("http://weixin.sogou.com/antispider/?from=" + EncodeUtil.encodeURL(fromUrl,
                    "UTF-8"));
            mss.put("Connection", "keep-alive");
            mss.put("Host", "weixin.sogou.com");
            String vcode = getVerifyImage("http://weixin.sogou.com/antispider/util/seccode.php", mss);
            Map<String, String> param = new HashMap<String, String>();
            param.put("c", vcode);
            param.put("r", location.replaceAll("http\\://weixin\\.sogou\\.com/antispider/\\?from=",""));
            param.put("v", "5");
            CloseableHttpResponse response = RequestSogouVerifyCode
                    .sendAntispider("http://weixin.sogou.com/antispider/?from=" + EncodeUtil.encodeURL(
                            fromUrl,
                            "UTF-8"), param);
            System.out.println("提交验证码,状态:" + response.getStatusLine().getStatusCode());

            Map<String,Object> resultMap = StringUtil.jsonToMap(HttpResponseUtil.getHtml(response));
            System.out.println("返回结果:" +StringUtil.mapToJson(resultMap));
            Map<String,Object> cookieMap = new HashMap<String,Object>();
            String cacheCookie = FileUtil.txt2String(new File("D:/sogou.txt"));// 读取保存在本地的已经登陆的Cookie信息
            cacheCookie = cacheCookie.replaceAll(" ", "");
            String[] cookieArgs = cacheCookie.split(";");
            for (String string : cookieArgs) {
                String[] n_v = string.split("=");
                if(n_v.length<2){
                    cookieMap.put(n_v[0],"");
                }else{
                    cookieMap.put(n_v[0], n_v[1]);
                }
            }
            for(Header header:response.getAllHeaders()){
                if("Set-Cookie".equals(header.getName())){
                    String value = header.getValue();
                    String[] valueStr = value.split(";");
                    String[] n_v_s = valueStr[0].split("=");
                    if (n_v_s.length>1){
                        cookieMap.put(n_v_s[0], n_v_s[1]);
                    }
                }
            }
            String cookieStrCache = "";
            int cacheIndex = 1;
            for(String key : cookieMap.keySet()){
                cookieStrCache+=(key+"="+cookieMap.get(key).toString());
                if(cacheIndex!=cookieMap.keySet().size()){
                    cookieStrCache+=";";
                }
                cacheIndex++;
            }
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter("D:/sogou.txt"));
                out.write(cookieStrCache);
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if("0".equals(resultMap.get("code").toString())){
                try {
                    Thread.sleep(10*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return connect(url);
            }else{
                try {
                    Thread.sleep(10*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return connect(url);
            }
        }
        return html1;
    }

    public void parse() {
        List<Map<String, Object>> lwa = new ArrayList<Map<String, Object>>();
        Document document = Jsoup.parse(html);
        Elements li_elements = document.select("#main>div>ul>li");

        List<TaskEntry> taskEntries = new ArrayList<TaskEntry>();

        for (Element element : li_elements) {
            try {
                Element title_element = element.select("li>div.txt-box>h3>a").first();
                System.out.println("标题:" + StringUtil.element2text(title_element) + "\t链接:"
                        + StringUtil.cleanEncode(title_element.attr("href")));
                Element content = element.select("li>div.txt-box>p.txt-info").first();
                String content_str = StringUtil.element2text(content);
                System.out.println("内容:" + content_str);
                Element nick_name_ele = element.select("li>div.txt-box>div.s-p>a").first();
                String nickname = StringUtil.element2text(nick_name_ele);
                Element createtimeEle = element.select("li>div.txt-box>div.s-p").first();
                String createtime = createtimeEle.attr("t");
                System.out.println("作者:" + nickname + "\t时间:" + createtime);

                TaskEntry taskEntry = new TaskEntry();

                taskEntry.setTaskID((Integer) mso.get("taskID"));
                //taskEntry.setInitAction("MouldWeixinInfo");
                taskEntry.setEntryUrl(StringUtil.cleanEncode(title_element.attr("href")));
                taskEntry.setScore(0.0d);

                taskEntries.add(taskEntry);

                Thread.sleep(20000);
            } catch (Exception e){
                e.printStackTrace();
            }


        }

        this.submitobj.submitTaskEntry("MouldWeixinInfo",taskEntries);
        String jsonData = JSON.toJSONString(taskEntries);
        System.out.println("发送数据:" + jsonData);
        /*try {
            submitData(jsonData);
        } catch (IOException e) {
            e.printStackTrace();
        }*/
        //System.out.println(html);
    }

    public void submit() {

    }

    public void submitData(String jsonData) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(MainControl.schedulerAddr+"/services/schedule/addEntry4http");

        StringEntity params = new StringEntity(jsonData.toString());
        params.setContentEncoding("UTF-8");
        httpPost.setEntity(params);

        CloseableHttpResponse res = httpclient.execute(httpPost);

        System.out.println("发送结果响应码:" + res.getStatusLine().getStatusCode());
        System.out.println("发送结果返回值为:" + res.getEntity().getContent().toString());
    }


    public static String getVerifyImage(String url, Map<String, String> mss) {
        String code = "";
        try {
            byte[] imgbt = HttpUtil.GetImage(url, 10000, mss);
            code = CodeUtil.getCode("3060", imgbt);
            System.out.println("验证码:" + code);
        } catch (Exception e) {
            return null;
        } finally {
        }
        return code;
    }

    public Map<String, String> requestHeader() {
        Map<String, String> requestHeader = new HashMap<String, String>();
        requestHeader.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        requestHeader.put("Accept-Encoding", "gzip, deflate, sdch");
        requestHeader.put("Accept-Language", "zh-CN,zh;q=0.8");
        String cookie = FileUtil.txt2String(new File("D:/sogou.txt"));// 读取保存在本地的已经登陆的Cookie信息
        requestHeader.put("Cookie", cookie);
        requestHeader.put("Host", "weixin.sogou.com");
        requestHeader.put("Proxy-Connection", "keep-alive");
        requestHeader.put("Referer",
                "http://weixin.sogou.com/weixin?type=2&query=%E8%8D%A3%E6%94%BE&ie=utf8&s_from=input&_sug_=y&_sug_type_=&w=01019900&sut=4809&sst0=1493866173053&lkt=3%2C1493866169793%2C1493866172952");
        requestHeader.put("Upgrade-Insecure-Requests", "1");
        requestHeader.put("User-Agent",
                "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
        return requestHeader;
    }
}
